package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.order.utils.GlobalIdGeneratorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zhuqiushi
 * @since 2022/09/20 20:57
 */
@RestController
@RequestMapping("/shopping")
public class OrderController {
    @Autowired
    GlobalIdGeneratorUtil globalIdGeneratorUtil;
    @Autowired
    OrderCoreService orderCoreService;
    @Autowired
    OrderQueryService orderQueryService;

    /*
     * 创建订单
     * */
    @PostMapping("/order")
    public ResponseData create(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest request) {
        //利用pipeline来对订单的各个步骤进行出路
    //   首先从head里获取user信息
        Object user_info = request.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject((String) user_info);
        Long uid = Long.valueOf(jsonObject.get("uid").toString());
        createOrderRequest.setUserId(uid);
        String uniqueKey = globalIdGeneratorUtil.getNextSeq("uniqueKey", 1);
        createOrderRequest.setUniqueKey(uniqueKey);
    //    之后调用service层来处理
        String order = orderCoreService.createOrder(createOrderRequest);
    //    封装后返回
        return new ResponseUtil().setData(order);
    }

    /*
     * 查询订单
     * */
    @GetMapping("/order")
    public ResponseData orderList(OrderListRequest orderListRequest, HttpServletRequest request) {
        //   首先从head里获取user信息
        Object user_info = request.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject((String) user_info);
        Long uid = Long.valueOf(jsonObject.get("uid").toString());
        orderListRequest.setUserId(uid);
        //调用service层来查询订单
        OrderListResponse orderListResponse = orderQueryService.orderList(orderListRequest);
        //封装返回
        return new ResponseUtil().setData(orderListResponse);
    }

    /*
     * 查询订单详情
     * */
    @GetMapping("/order/{orderId}")
    public ResponseData orderDetail(@PathVariable String orderId, HttpServletRequest request) {
        //   首先从head里获取user信息
        Object user_info = request.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject((String) user_info);
        Long uid = Long.valueOf(jsonObject.get("uid").toString());
        //调用service层来查询订单
        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(orderId);
        //封装返回
        return new ResponseUtil().setData(orderDetailResponse);
    }

    /*
     * 取消订单
     * */
    @PostMapping("/cancelOrder")
    public ResponseData cancelOrder(@RequestBody Map map, HttpServletRequest request) {
        //从map中获取要取消的订单id
        String cancelOrderId = (String) map.get("orderId");
        //调用service层操作
        orderCoreService.cancelOrder(cancelOrderId);
        return new ResponseUtil().setData(null);
    }

    /*
     * 删除订单
     * */
    @DeleteMapping("/order/{orderId}")
    public ResponseData deleteOrder(@PathVariable String orderId) {
        //调用service
        orderCoreService.deleteOrder(orderId);
        return new ResponseUtil().setData(null);
    }
}
